package com.ruoyi.omsa.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.omsa.domain.OmSa;
import com.ruoyi.omsa.service.IOmSaService;
import com.ruoyi.omsampuser.domain.OmSampuser;
import com.ruoyi.omsampuser.service.IOmSampuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * 文章管理Controller
 * 
 * @author ruoyi
 * @date 2025-02-21
 */
@RestController
@RequestMapping("/mp/om/sa")
public class MpOmSaController extends BaseController
{
    @Autowired
    private IOmSaService omSaService;

    @Autowired
    private IOmSampuserService omSampuserService;

   //所有文章-翻页

    @GetMapping("/list")
    public TableDataInfo list(OmSa omSa)
    {
        startPage();
        List<OmSa> list = omSaService.selectOmSaList(omSa);
        return getDataTable(list);
    }

   //收藏文章动作，就是插入
   @PostMapping(value = "/sainfo")
   public AjaxResult sainfo(@RequestBody HashMap<String,Object> in)
   {
       //saId
       //return success(omSaService.selectOmSaBySaId(saId));
       if(null == in.get("said")){
         return error("文章id没有传");
       }
       Long saId = Long.valueOf(in.get("said").toString());
       OmSa omSa = omSaService.selectOmSaBySaId(saId);
       if(null == omSa){
         return error("没有此文章");
       }
       if(null != in.get("openid")){
           //omSampuserService.checkinfo();
           //返回一行数据
           String openId = in.get("openid").toString();
           //HashMap<String,Object> m = omSampuserService.getCheckInfoMap(saId,openId);
           OmSampuser omSampuser = omSampuserService.getCheckInfo(saId,openId);
           omSa.setOmSampuser(omSampuser);
       }
       return success (omSa);

   }

   //我的所有收藏的文章列表
   @GetMapping(value = "/ttmysaList?{pageNum}")
   public List<OmSa> mysaList2(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize, @PathVariable("openId") String openId){
        //list?pageNum=2&pageSize=10&openId=abc
       int offset = (pageNum - 1) * pageSize;
       List<OmSa> list = omSaService.mysaList(openId,pageSize,offset);
        return list;
   }

   // @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
   @GetMapping(value = "/mysaList")
   public TableDataInfo mysaList(  @RequestParam(value = "pageNum") int pageNum,
                                @RequestParam(value = "pageSize") int pageSize,
                                @RequestParam(value = "openId") String openId
   ){
       //list?pageNum=2&pageSize=10&openId=abc
       int offset = (pageNum - 1) * pageSize;
       List<OmSa> list = omSaService.mysaList(openId,pageSize,offset);
       return getDataTable(list);
   }

   //小卢说left join 也可以 startPage() 没时间试
}
